/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chuiniurobot.message;

import chuiniurobot.ChuiniuRobot;
import chuiniurobot.GameStatusEnum;
import com.chuiniu.server.command.BaseCommand;
import java.util.HashMap;
import java.util.Map;
import org.apache.mina.core.session.IoSession;

/**
 *
 * @author zhengji
 */
public abstract class BaseMessage extends BaseCommand{

    protected ChuiniuRobot robot;
    public BaseMessage(IoSession ioSession, String requestJSONStr) {
        super(ioSession, requestJSONStr);
    }
    
    public void setRobot(ChuiniuRobot robot){
        this.robot=robot;
    }
    public ChuiniuRobot getRobot(){
        return this.robot;
    }
    public GameStatusEnum getGameStatus(){
        return this.robot.getGameStatus();
        
    }
    
    public void setGameStatus(GameStatusEnum status){
        this.robot.setGameStatus(status);
    }
    
    /**
     * 获得当前玩家标识
     */
    public int getPlayerId(){
       return  this.getRobot().getPlayerId();
    }
    public void setPlayerId(int playerId){
        this.getRobot().setPlayerId(playerId);
    }
    
      /**
     * 获得当前对战玩家标识
     */
    public int getVSPlayerId(){
        return this.getRobot().getVSPlayerId();
    }
    public void setVSPlayerId(int playerId){
        this.getRobot().setVSPlayerId(playerId);
    }
    
   protected String toStringSplitByComma(String[] array)
   {
        if(array==null || array.length==0) return "";
        StringBuilder buffer=new StringBuilder();
        for(int i=0;i<array.length;i++){
            buffer.append(array[i] +",");
        }
        
        return buffer.toString().substring(0, buffer.toString().length()-1);
    }
    
   
   
   public int getCurrentGuessAmount() {
       return   this.getRobot().getCurrentGuessAmount();
    }

    public void setCurrentGuessAmount(int currentGuessAmount) {
        this.getRobot().setCurrentGuessAmount(currentGuessAmount);
    }

    public int getCurrentGuessNumber() {
        return this.getRobot().getCurrentGuessNumber();
    }

    public void setCurrentGuessNumber(int currentGuessNumber) {
        this.getRobot().setCurrentGuessNumber(currentGuessNumber);
    }

    public String[] getSelfDiceNumbers() {
        return this.getRobot().getSelfDiceNumbers();
    }

    public void setSelfDiceNumbers(String[] selfDiceNumbers) {
        this.getRobot().setSelfDiceNumbers(selfDiceNumbers);
    }

    public String[] getVsPlayerDiceNumbers() {
        return this.getRobot().getVsPlayerDiceNumbers();
    }

    public void setVsPlayerDiceNumbers(String[] vsPlayerDiceNumbers) {
       this.getRobot().setVsPlayerDiceNumbers(vsPlayerDiceNumbers);
    }
    
    
     public boolean isHasGuessOne() {
        return this.getRobot().isHasGuessOne();
    }

    public void setHasGuessOne(boolean hasGuessOne) {
        this.getRobot().setHasGuessOne(hasGuessOne);
    }
    
      /**
     * 获得点数在自己所有骰子中得数量
     * @param number 点数
     * @return 
     */
    protected int getNumberCountInSelfDice(int number){
       
           return this.getNumberCountInArray(number, this.getSelfDiceNumbers());
  
      
    }
    
      /**
     * 获得点数在对家所有骰子中得数量
     * @param number 点数
     * @return 
     */
    protected int getNumberCountInVSPlayerDice(int number){
        
        return this.getNumberCountInArray(number, this.getVsPlayerDiceNumbers());
        
     
    }
    private int getNumberCountInArray(int number,String[] strArray){
          int  count=0;
        for(String str:strArray){
            if(str.equals(String.valueOf(number))){
                count ++;
            }else if(str.equals("1") && !this.isHasGuessOne()){
            //1 具备万能效果
              count ++;
            
           }
        }
        
        return count;
    }
    
      /**
     * 发送猜测结果
     */
    protected  void sendGuessNumber() {
 
        Map<String,String> map=new HashMap<String,String>();
        
        map.put("cmd", BaseMessage.GuessDataCommand);
        map.put("id", String.valueOf(this.getPlayerId()));
        
        map.put("d", String.format("%d,%d", this.getCurrentGuessAmount(),this.getCurrentGuessNumber()));
        
        this.sendMessage(map);
    }
}
